<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/17
 * Time: 11:29
 */

namespace App\Exports;
use Exception;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithEvents;     // 自动注册事件监听器
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;    // 导出 0 原样显示，不为 null
use Maatwebsite\Excel\Events\AfterSheet;
class DriverExports implements FromCollection, WithEvents, WithStrictNullComparison
{
    private $row;
    private $data;
    public $rowNum;

    public function __construct($row,$data)
    {

        $this->row = $row;
        $this->data = $data;

//        dd($data);
    }

    /**
     * registerEvents.
     * 事件监听
     * @return array
     */
    public function registerEvents(): array
    {
        return [
            // 生成表单元后处理事件
            AfterSheet::class => function (AfterSheet $event) {
                // 设置单元格内容居中
                $event->sheet->getDelegate()->getStyle('A1:Z1265')->getAlignment()->setVertical('center');
                //设置区域单元格水平居中
                $event->sheet->getDelegate()->getStyle('A1:Z1265')->getAlignment()->setHorizontal('center');
                //设置字体大小
                $event->sheet->getDelegate()->getStyle('A1:Z1265')->getFont()->setSize(11);
                // 定义列宽度
                $widths = ['A' => 10, 'B' => 20, 'C' => 30, 'D' => 25, 'E' => 23, 'F' => 14, 'G' => 25, 'H' => 45,'I' => 50];
                foreach ($widths as $k => $v) {
                    // 设置列宽度
                    $event->sheet->getDelegate()->getColumnDimension($k)->setWidth($v);
                }
                //设置行高，$i为数据行数
                for ($i = 0; $i <= $this->rowNum; $i++) {
                    $event->sheet->getDelegate()->getRowDimension($i)->setRowHeight(25);
                }
                // 设置表中字体
                $event->sheet->getDelegate()->getStyle('A1:Z1265')->applyFromArray([
                    'font' => [
                        'name' => '宋体',
                        'bold' => false,
                        'italic' => false,
                        'strikethrough' => false,
                        'color' => [
                            'rgb' => '000000',
                        ],
                    ],
                ]);

            },
        ];

    }



    /**
     * collection.
     *
     * @return Collection
     *
     * @throws Exception
     */
    public function collection()
    {
        $row = $this->row;
        $data = $this->data;

        $parms = [];
        foreach ($data as $v){
            $parms[]=[
                'driver_id'=>$v['driver_id'],
                'driver_name'=>$v['driver_name'],
                'phone'=>"\t" .$v['phone'],
                'identity'=>"\t" .$v['identity'],
                'status'=>$v['status'],
                'company_name'=>$v['company_name'],
                'merger_name'=>$v['merger_name'],
                'created_at'=>$v['created_at']
            ];
        }

        //设置表头
        foreach ($row[0] as $key => $value) {
            $key_arr[] = $key;
        }
        //输入数据
        foreach ($parms as $item) {
            array_push($row, (array)$item);
        }

        return collect($row);
    }
}